package com.weijing.pretty_app.adapter

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.core.content.ContextCompat
import androidx.recyclerview.widget.RecyclerView
import com.weijing.pretty_app.R
import com.weijing.pretty_app.databinding.ItemNenFuBinding

class NenFuAdapter(private val list: List<String>) :
    RecyclerView.Adapter<NenFuAdapter.NenFuHolder>() {
    private var mPosition: Int = 0

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): NenFuHolder {
        return NenFuHolder(
            ItemNenFuBinding.inflate(
                LayoutInflater.from(parent.context), parent, false
            )
        )
    }

    override fun getItemCount(): Int {
        return list.size
    }

    override fun onBindViewHolder(holder: NenFuHolder, position: Int) {
        val name = list[position]
        with(holder) {
            if (mPosition == position) {
                binding.itemNenFuTv.setTextColor(
                    ContextCompat.getColor(binding.root.context, R.color.white)
                )
                binding.itemNenFuCl.setBackgroundResource(R.drawable.shape_whit_6)
                if (position % 2 == 0) { //左侧
                    binding.triangleIvLeft.visibility = View.VISIBLE
                    binding.triangleIvRight.visibility = View.INVISIBLE
                } else {
                    binding.triangleIvRight.visibility = View.VISIBLE
                    binding.triangleIvLeft.visibility = View.INVISIBLE
                }
            } else {
                binding.itemNenFuTv.setTextColor(
                    ContextCompat.getColor(binding.root.context, R.color.color_FFD754)
                )
                binding.itemNenFuCl.setBackgroundResource(R.drawable.shape_ffd754_6)

                binding.triangleIvRight.visibility = View.INVISIBLE
                binding.triangleIvLeft.visibility = View.INVISIBLE
            }
            binding.itemNenFuTv.text = name

            if (onClickListener != null) {
                holder.itemView.setOnClickListener {
                    onClickListener?.onItemClickListener(position, name)
                }
            }
        }
    }

    class NenFuHolder(val binding: ItemNenFuBinding) : RecyclerView.ViewHolder(binding.root)

    var onClickListener: OnClickListener? = null

    interface OnClickListener {
        fun onItemClickListener(position: Int, name: String)
    }

    fun setIndex(position: Int) {
        mPosition = position
        notifyDataSetChanged()
    }
}